<template>
  <div class="clubsListBox">
    <!-- 搜索表单 -->
    <div>
      <el-form :model="queryData" ref="queryForm" size="small" :inline="true">
        <el-form-item label="社团">
          <el-select
            v-model="queryData.clubId"
            clearable
            filterable
            placeholder="请选择社团"
            style="width: 240px"
          >
            <el-option
              v-for="item in club"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <!-- 公告列表 -->
    <div v-if="formData.length">
      <div
        v-for="(item, index) in formData"
        :key="index"
        class="activite-box"
        :class="{ 'last-activite-box': index === formData.length - 1 }"
      >
        <div class="activite-subject">
          <div
            style="
              position: relative;
              display: flex;
              align-items: center;
              width: 100%;
            "
          >
            <img
              v-if="item.image"
              class="notice-image"
              alt="图片异常显示"
              :src="item.image"
            />
            <div style="margin-left: 15px; flex: 1">
              <div class="notice-title">{{ item.title }}</div>
              <div class="notice-meta">
                <span class="club-tag">{{ item.club || "未知社团" }}</span>
                <span class="publish-time">发布时间：{{ item.gmtCreate }}</span>
              </div>
            </div>
          </div>
          <el-button
            type="primary"
            round
            class="activite-button"
            @click="goNoticeDetails(item)"
            >查看详情</el-button
          >
        </div>
        <div class="notice-content">
          <span v-html="truncateContent(item.content)"></span>
          <!-- {{ truncateContent(item.content) }} -->
        </div>
      </div>

      <!-- 分页 -->
      <pagination
        class="pagination"
        v-show="total > 0"
        :total="total"
        :page.sync="queryData.pageNum"
        :limit.sync="queryData.pageSize"
        @pagination="getList"
      />
    </div>
    <div class="no-data" v-else>暂无公告</div>
  </div>
</template>

<script>
import { select } from "@/api/organizations/index.js";
import { getAnnouncementLists } from "@/api/cms/cms.js";
import Pagination from "@/components/Pagination/index.vue";
export default {
  components: {
    Pagination,
  },
  data() {
    return {
      club: [],
      queryData: {
        clubId: "",
        pageNum: 1,
        pageSize: 10,
      },
      rowData: [
        {
          prop: "title",
          label: "公告名称",
          width: "200",
        },

        {
          prop: "image",
          label: "image",
          width: "200",
        },
        {
          prop: "club",
          label: "社团",
          width: "200",
        },
        {
          prop: "content",
          label: "公告内容",
        },
      ],
      formData: [],
      total: 0,
    };
  },
  async created() {
    await this.getClubsByPage();
    this.getAnnouncementLists();
  },
  methods: {
    truncateContent(content) {
      // 检测内容长度并截断
      if (content?.length > 150) {
        content = content.substring(0, 150) + "...";
      }

      // 替换 <img> 标签，添加 style="width:100px"
      content = content.replace(/<img[^>]*>/g, (imgTag) => {
        // 检查是否已经有 style 属性
        if (imgTag.includes('style="')) {
          // 如果已经有 style 属性，追加样式
          return imgTag.replace(/style="([^"]*)"/, 'style="$1; display:none"');
        } else {
          // 如果没有 style 属性，直接添加
          return imgTag.replace(/<img/, '<img style="display:none"');
        }
      });

      return content;
    },
    getClubsByPage(data) {
      return new Promise((resolve) => {
        select({
          pageNum: 1,
          pageSize: 99999999,
          data,
        }).then((res) => {
          const { JoinClubsByPage } = res.data;
          this.club = JoinClubsByPage.records;
          resolve();
        });
      });
    },
    getAnnouncementLists() {
      return new Promise((resolve) => {
        getAnnouncementLists(
          this.queryData.pageNum,
          this.queryData.pageSize,
          this.queryData
        ).then((res) => {
          if (res.code == 20000) {
            this.formData = res.data.list.records;
            this.total = res.data.list.total;
            // this.activityData = res.data.list
            this.formData.forEach((e) => {
              const club = this.club.filter((es) => es.id == e.clubId);
              e.club = club.length ? club[0].name : "";
            });
            resolve(this.formData);
          }
        });
      });
    },
    handleQuery() {
      this.queryData.pageNum = 1;
      this.getAnnouncementLists();
    },
    getList(e) {
      this.queryData.pageNum = e.page;
      this.queryData.pageSize = e.limit;
      this.getAnnouncementLists();
    },
    goNoticeDetails(e) {
      this.$router.push(`/noticeDetails?id=${e.id}`);
    },
  },
};
</script>

<style scoped>
/* 统一基础样式 */
.clubsListBox {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
  font-family: "Helvetica Neue", Arial, sans-serif;
}

/* 搜索表单 */
::v-deep .el-form {
  background: #f8f9fa;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  margin-bottom: 30px;
}

::v-deep .el-select .el-input__inner {
  border-radius: 20px;
}

::v-deep .el-button--primary {
  background: linear-gradient(135deg, #409eff, #3375b9);
  border: none;
  border-radius: 20px;
  padding: 9px 20px;
  transition: all 0.3s;
}

/* 公告卡片 */
.activite-box {
  background: #fff;
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 3px 15px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s;
  border: 1px solid #ebeef5;
}

.activite-box:hover {
  transform: translateY(-3px);
}

.activite-subject {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

/* 图片样式 */
.notice-image {
  width: 120px;
  height: 80px;
  border-radius: 8px;
  object-fit: cover;
  border: 1px solid #eee;
}

/* 标题区域 */
.notice-title {
  font-size: 1.2rem;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 8px;
}

/* 元信息 */
.notice-meta {
  display: flex;
  align-items: center;
  gap: 15px;
}

.club-tag {
  background: #409eff;
  color: #ffffff;
  padding: 4px 12px;
  border-radius: 15px;
  font-size: 0.85rem;
}

.publish-time {
  color: #909399;
  font-size: 0.9rem;
}

/* 内容区域 */
.notice-content {
  color: #606266;
  line-height: 1.7;
  margin-top: 15px;
  font-size: 0.95rem;
}

.notice-content img {
  width: 100px !important;
}

/* 按钮样式 */
.activite-button {
  background: linear-gradient(135deg, #409eff, #3375b9);
  border: none;
  padding: 8px 20px;
  border-radius: 20px;
  font-size: 0.9rem;
  box-shadow: 0 2px 6px rgba(64, 158, 255, 0.3);
}

.activite-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.4);
}

/* 分页 */
.pagination {
  margin-top: 30px;
  text-align: center;
}

/* 无数据提示 */
.no-data {
  font-size: 1.1rem;
  text-align: center;
  color: #909399;
  padding: 40px;
  background: #f8f9fa;
  border-radius: 8px;
  margin: 20px 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .activite-subject {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }

  .notice-image {
    width: 100%;
    height: 160px;
  }

  .activite-button {
    width: 100%;
    text-align: center;
    margin-top: 15px;
  }

  .notice-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
}
</style>